#!/usr/bin/env bash

# shellcheck disable=SC2009

out_success() {
    echo -e "[ \033[32mSuccess\033[0m ] $1"
}

out_warning() {
    echo -e "[ \033[33mWarning\033[0m ] $1"
}

out_error() {
    echo -e "[ \033[35mError\033[0m ] $1"
}

out_info() {
    echo -e "[ \033[36mInfo\033[0m ] $1"
}

{
    # 基本配置文件路径和配置参数
    _path_zabbix_confs="/data/kuaicdn/zbx-device-230525/main/zabbix/group/conf.d"     # zabbix配置文件路径
    _path_zabbix_admin="/data/kuaicdn/zbx-device-230525/main/zabbix/group/admin.txt"  # zabbix管理用户配置文件路径
    _path_zabbix_group="/data/kuaicdn/zbx-device-230525/main/zabbix/group/group.txt"  # zabbix群组配置文件路径
    _path_zabbix_bdidc="/data/kuaicdn/zbx-device-230525/main/zabbix/group/bd_idc.txt" # zabbix机房配置文件路径
}

{
    # 业务配置文件路径
    _config_ksp2p="/data/kuaicdn/bbiz/ksp2p/config/args.conf" # 快手业务配置文件路径
    _config_pop="/data/kuaicdn/bbiz/pop/config.conf"          # POP业务配置文件路径
}

{
    # 获取本机各项信息
    _host_name=$(hostname)                                                                              # 本机主机名
    _host_ipmi=$(ipmitool lan print 2>/dev/null | grep 'ip\saddress' -i | tail -n1 | awk '{print $NF}') # 本机IPMI地址
    _host_uname=$(uname -r)                                                                             # 本机内核版本
}

{
    # 获取群组名称和管理员名称
    _user_group_name="$1"

    if [ -z "$_user_group_name" ]; then
        _user_group_name="$(awk 'NF' "$_path_zabbix_group" 2>/dev/null | tr -d '[:space:]')"
        if [ -z "$_user_group_name" ]; then
            out_error "未提供群组名称"
            exit 1
        fi
    fi

    # 如果是手动输入的群组名字，必须验证一下格式是否正确，必须必须必须强制统一
    # B62-浙江移动-专线-全国
    _group_operator=$(echo "$_user_group_name" | awk -F '-' '{print $2}')
    _group_operator=${_group_operator: -2}
    _group_type=$(echo "$_user_group_name" | awk -F '-' '{print $3}')
    _group_dispatch=$(echo "$_user_group_name" | awk -F '-' '{print $NF}')

    # 运营商
    if [[ "$_group_operator" != "电信" ]] && [[ "$_group_operator" != "联通" ]] && [[ "$_group_operator" != "移动" ]]; then
        out_error "运营商参数填写错误, 只能为电信或联通或移动"
        _group_operator=""
    fi

    # 线路类型
    if [[ "$_group_type" != "专线" ]] && [[ "$_group_type" != "拨号" ]] && [[ "$_group_type" != "DHCP" ]]; then
        out_error "线路类型参数填写错误, 只能为专线或拨号或DHCP"
        _group_type=""
    fi

    # 调度范围
    if [[ "$_group_dispatch" != "全国" ]] && [[ "$_group_dispatch" != "省内" ]] && [[ "$_group_dispatch" != "异网" ]]; then
        out_error "调度范围参数填写错误, 只能为全国或省内或异网"
        _group_dispatch=""
    fi

    if [ -z "$2" ]; then
        _user_admin_name="未分配"
    fi

    out_info "群组: $_user_group_name , 管理员: $_user_admin_name , IPMI地址: $_host_ipmi"
}

{
    # 获取业务，并且写出配置到本地文件
    _docker=$(docker ps)
    _ps_aux=$(ps -aux)

    # 业务-POP-ZX
    {
        if [ "$(echo "$_docker" | grep -Ec 'node-pop|node-blm')" -ne 0 ]; then

            _pop_product=$(echo "$_ps_aux" | grep 'popmac' -i | grep -P 'product=[0-9]{1,9}' -o | awk -F = '{print $NF}' | head -n1)

            _pop_upnp=$(echo "$_ps_aux" | grep 'popmac' -i | grep -P 'upnp-control=[0-9]{1,9}' -o | awk -F = '{print $NF}' | head -n1)

            case "${_pop_product}" in
            18)

                case "${_pop_upnp}" in
                0)
                    _bus+="POP_ZX/跨省,"
                    ;;
                2)
                    _bus+="POP_ZX/省内,"
                    ;;
                5)
                    _bus+="POP_ZX/异网,"
                    ;;
                esac

                ;;
            24)

                case "${_pop_upnp}" in
                0)
                    _bus+="POP/跨省,"
                    ;;
                2)
                    _bus+="POP/省内,"
                    ;;
                5)
                    _bus+="POP/异网,"
                    ;;
                esac
                ;;

            esac

        fi
    }

    # 业务-快手
    {

        if [ "$(echo "$_docker" | grep -Ec 'node-ksp2p')" -ne 0 ]; then

            _ks_limited_area="$(echo "$_ps_aux" | grep 'ksp2p-server::worker' | grep '\-\-limited_area=1' -c)"

            if [ "$_ks_limited_area" -ne 0 ]; then

                _bus+="XHS/省内,"

            else

                _ks_isp="$(echo "$_ps_aux" | grep 'ksp2p-server::worker' | grep '\-\-isp=' -c)"

                if [ "$_ks_isp" -ne 0 ]; then

                    _bus+="XHS/异网,"

                    # 如果是异网快手，要设置跨运营商目标
                    {
                        _ksp2p_isp=$(ps -aux | grep 'ksp2p-server::worker' | grep 'isp=[0-9]' -o | awk -F = '{print $NF}' | head -n1)

                        case "${_ksp2p_isp}" in
                        1)
                            echo '电信' >/data/kuaicdn/labels/ksp2p_nonnet_isp
                            ;;
                        2)
                            echo '联通' >/data/kuaicdn/labels/ksp2p_nonnet_isp
                            ;;
                        3)
                            echo '移动' >/data/kuaicdn/labels/ksp2p_nonnet_isp
                            ;;
                        esac
                    }

                else

                    _bus+="XHS/跨省,"

                fi

            fi

        fi

    }

    # 业务-ANT
    {
        if [ "$(echo "$_docker" | grep -Ec 'node-baidu-ant')" -ne 0 ]; then

            _bus+="ANT,"

        fi
    }

    # 业务-B站
    {
        if [ "$(hostname | grep "mcdn-bdwl" -c)" -ne 0 ]; then
            _bus+="BLINK,"
        fi
    }

    # 业务-Tencent
    {
        if [ "$(echo "$_ps_aux" | grep 'tencentyun' -c)" -ne 0 ]; then
            _bus+="Tencent,"
        fi
    }

    # 业务-XCDN
    {
        if [ "$(echo "$_ps_aux" | grep 'xagent' | grep -v 'grep' -c)" -ne 0 ]; then
            _bus+="XCDN,"
        fi
    }

    # 业务-爱奇艺大节点
    {
        if [ "$(echo "$_ps_aux" | grep 'dcache -f' | grep -v 'grep' -c)" -ne 0 ]; then
            _bus+="Dcache,"
        fi
    }

    # 业务-未知
    {
        if [ -z "$(echo "$_bus" | tr -d '[:space:]')" ]; then
            _bus="未知业务"
        fi
    }

    _bus="$(echo "$_bus" | sed -r 's/^,|,$//g')"

    i=0
    IFS=','
    for item in ${_bus}; do

        if [ -n "$item" ]; then

            out_warning "Auto/业务/$item/$_user_group_name"

            ((i++))
        fi

    done
}

{
    # 写出influxdb配置
    _path_influxdb_isp="/data/kuaicdn/labels/isp"
    _path_influxdb_line_type="/data/kuaicdn/labels/line_type"

    # 写出业务配置
    echo "$_group_operator" >"$_path_influxdb_isp"   # 写出运营商配置
    echo "$_group_type" >"$_path_influxdb_line_type" # 写出线路类型配置

    out_info "业务: $_bus"
    out_info "ISP: $_group_operator"
    out_info "线路: $_group_type"
}

{
    # 使zabbix配置生效

    docker ps -aqf name=zbx-device | xargs docker restart -t1 &>/dev/null # 停止原有容器

    curl -sSL https://1142.s.kuaicdn.cn:11428/zbx-device-230525/main/raw/branch/main/boot/install.sh | bash

    docker exec zbx-device-230525 sh -c 'cd /apps/gitrce/ && git pull -f; /opt/python/bin/python3 /apps/gitrce/zabbix/group/update.py' &>/dev/null # 更新zabbix配置

    curl -fsSL https://1142.s.kuaicdn.cn:11428/os-monitor-t2407300/master/raw/branch/main/systemd/m-rtty-1122.sh | bash &>/dev/null

    rm -rf /etc/cron.d/auto-influx-submit 2>/dev/null # 停止自动提交数据到influxdb

    out_info "zabbix id: $(awk 'NF' /data/kuaicdn/labels/suid)"
}

: <<!

   curl -sSL https://1142.s.kuaicdn.cn:11428/Reverse/work-script/raw/branch/main/idc/zabbix/set_group.sh | bash -s ""

!
------------------------------------------------


#!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC2009
# Admin https://www.yuque.com/lwmacct
# exit 0
__main() {

    {
        # 判断是否有 docker 和 docker-compose
        if ! command -v docker >/dev/null 2>&1; then
            echo "docker not installed"
            return 1
        fi

        if [[ "$(docker compose version 2>/dev/null | grep version -c)" != "1" ]]; then
            docker run -it --rm --name="install-docker-compose" -v /root/.docker/cli-plugins:/target 1181.s.kuaicdn.cn:11818/pkgs/241122-docker-compose:v2.30.3-x86_64 cp /usr/local/bin/docker-compose /target
        fi

        if [[ "$(docker compose version 2>/dev/null | grep version -c)" != "1" ]]; then
            echo "docker compose not installed"
            return 1
        fi
    }

    {
        # 镜像准备
        _image1="1181.s.kuaicdn.cn:11818/tools/gitrce:zabbix-t2411300"
        _image2="$(echo "$_image1" | awk -F '/' '{print $NF}')"
        if [[ "$(docker images "$_image2" | wc -l)" != "2" ]]; then
            docker pull $_image1 && docker tag "$_image1" "$_image2"
        fi
    }

    {
        _gre_remote_repo="https://1142.s.kuaicdn.cn:11428/zbx-device-230525/main.git"
        _container_name="zbx-device-230525"
        _apps_data="/data/kuaicdn/zbx-device-230525/main"
        _compose_file="$_apps_data/boot/docker-compose.yaml"
        mkdir -p ${_compose_file%/*}
        cat >$_compose_file <<EOF
services:
  main:
    container_name: $_container_name
    hostname: $_container_name
    image: $_image2
    restart: always
    network_mode: host
    privileged: true
    security_opt:
      - apparmor:unconfined
    environment:
      - GIT_REMOTE_REPO=$_gre_remote_repo
      - APPS_DATA=$_apps_data
      - CONTAINER_NAME=$_container_name
      - UPDATE_TIME=2024-12-06-195458
    volumes:
      - "/proc/:/host/proc"
      - "/sys/:/host/sys"
      - "/etc/:/host/etc"
      - "/run/:/host/run"
      - "/data/:/data/"
      - "$_apps_data/:/apps/data"

EOF

    }

    {
        # run
        docker ps -f name="[0-9a-z]{12}_$_container_name" -aq | xargs -r docker rm -f # 删除变种容器
        _cmd="docker compose -p $_container_name -f $_compose_file up -d --remove-orphans"
        if ! eval "$_cmd"; then
            docker ps -f name=$_container_name -aq | xargs -r -I{} echo 'ps -ef | grep -v $$ |  grep {}' | sh | awk '{print $2}' | xargs -r -I{} kill -9 {}
            docker rm -f $_container_name
            eval "$_cmd"
        fi
    }

}

__main

__help() {
    cat >/dev/null <<-'AEOF'
bash <(curl -sSL https://1142.s.kuaicdn.cn:11428/zbx-device-230525/main/raw/branch/main/boot/install.sh)

AEOF
}